package minWindow.java;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class minWindow {
    public static void main(String[] args) {
        String s = "aaaaaaaaaaaabbbbbcdd";
        String t = "abcdd";
        System.out.println("value+"+minWindow(s,t));
    }

    public static String minWindow(String s, String t) {
        if (t.length() > s.length()) {
            return "";
        }

        int left = 0;
        String res = s;
        char[] sCharArray = s.toCharArray();
        char[] tCharArray = t.toCharArray();
        Map<String, Integer> tMap = new HashMap<>();
        boolean outFlag = false;
        for (int i = 0; i < t.length(); i++) {
            tMap.put(String.valueOf(tCharArray[i]),tMap.getOrDefault(String.valueOf(tCharArray[i]),0) + 1);
            if (!s.contains(String.valueOf(tCharArray[i]))) {
                outFlag = true;
            }
        }

        if (outFlag) {
            return "";
        }

        for (int right = t.length(); right <= sCharArray.length; right++) {
            boolean flag = true;
            while (flag) {
                Map<String, Integer> innerMap = new HashMap<>();
                boolean innerFlag = true;
                String tmpl = s.substring(left, right);
                for (int i = left; i < right; i++) {
                    innerMap.put(String.valueOf(sCharArray[i]),innerMap.getOrDefault(String.valueOf(sCharArray[i]),0) + 1);
                }
                int innerLeft = 0;
                while (innerLeft < t.length()) {
                    Integer innerNum = Objects.isNull(innerMap.get(String.valueOf(tCharArray[innerLeft]))) ? 0 : innerMap.get(String.valueOf(tCharArray[innerLeft]));
                    Integer outNum = Objects.isNull(tMap.get(String.valueOf(tCharArray[innerLeft]))) ? 0 : tMap.get(String.valueOf(tCharArray[innerLeft]));
                    // 大于等于算通过
                    if (innerNum >= outNum) {
                        innerFlag = true;
                        flag = true;
                        break;
                    }

                    if (innerNum < outNum) {
                        flag = false;
                        innerFlag = false;
                        break;
                    }
                    innerLeft++;
                }
                if (innerFlag) {
                    res = tmpl.length() < res.length() ? tmpl : res;
                }

                if (flag) {
                    left++;
                }
            }
        }
        return res;
    }
}
